package com.fulihui.usercore.core.repository.impl;

import com.alibaba.fastjson.JSONObject;
import com.fulihui.usercore.biz.conv.ThirdMerchantConv;
import com.fulihui.usercore.condition.ThirdMerchantCondition;
import com.fulihui.usercore.core.repository.ThirdMerchantRepository;
import com.fulihui.usercore.dal.dataobj.ThirdMerchantDO;
import com.fulihui.usercore.dal.mapper.ThirdMerchantMapper;
import com.fulihui.usercore.dto.ThirdMerchantDTO;
import com.fulihui.usercore.request.merchant.ThirdMerchantSaveRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author Willard.Hu on 2016/9/23 0023.
 */
@Repository
public class ThirdMerchantRepositoryImpl implements ThirdMerchantRepository {
    @Autowired
    private ThirdMerchantMapper thirdMerchantMapper;

    /**
     * @see ThirdMerchantRepository#insert(ThirdMerchantSaveRequest)
     */
    @Override
    public int insert(ThirdMerchantSaveRequest insert) {
        ThirdMerchantDO dataobj = ThirdMerchantConv.toDO(insert, null);
        thirdMerchantMapper.insert(dataobj);
        return dataobj.getId();
    }

    /**
     * @see ThirdMerchantRepository#update(ThirdMerchantSaveRequest)
     */
    @Override
    public boolean update(ThirdMerchantSaveRequest update) {
        ThirdMerchantDO old = thirdMerchantMapper.queryByPK(update.getId());
        return thirdMerchantMapper.update(ThirdMerchantConv.toDO(update,
            JSONObject.parseObject(old.getExtInfo())));
    }

    /**
     * @see ThirdMerchantRepository#delete(int)
     */
    @Override
    public boolean delete(int id) {
        return thirdMerchantMapper.delete(id);
    }

    /**
     * @see ThirdMerchantRepository#queryByPK(int)
     */
    @Override
    public ThirdMerchantDTO queryByPK(int id) {
        return ThirdMerchantConv.toDTO(thirdMerchantMapper.queryByPK(id));
    }

    /**
     * @see ThirdMerchantRepository#query(ThirdMerchantCondition)
     * @param condition 查询条件
     */
    @Override
    public List<ThirdMerchantDTO> query(ThirdMerchantCondition condition) {
        return ThirdMerchantConv
            .toList(thirdMerchantMapper.query(ThirdMerchantConv.toDO(condition)));
    }
}
